import Vue from 'vue'
import Vuex from 'vuex'
import cloneDeep from 'lodash/cloneDeep'
import common from './modules/common'
import user from './modules/user'
import app from './modules/app'
import dragConfig from './modules/dragConfig'
import getters from './getters'

Vue.use(Vuex)

export default new Vuex.Store({
  state: {
    authList: [],
    jumpUrlConfig: {
      mch_register: '',
      mch_forget_password: '',
      hf_management_url: '',
      fht_url: '',
      change_pwd: ''
    },
    selCompany: {},
    masterCompanyList: [],
    userInfo: {},
    path: [],
    curRouteName: ''
  },
  modules: {
    common,
    user,
    app,
    dragConfig
  },
  getters,
  mutations: {
    // 重置vuex本地储存状态
    resetStore (state) {
      Object.keys(state).forEach((key) => {
        state[key] = cloneDeep(window.SITE_CONFIG['storeState'][key])
      })
    },
    SET_USER_INFO (state, userInfo) {
      state.userInfo = userInfo
    },
    SET_PATH (state, path) {
      state.path = path
    },
    SET_CUR_ROUTE_NAME (state, name) {
      state.curRouteName = name
    }
  },
  actions: {
    async setUserInfo ({commit}, userInfo) {
      try {
        commit('SET_USER_INFO', userInfo)
      } catch (err) {
        console.log(err)
      }
    },
    async setPath ({commit}, path) {
      try {
        commit('SET_PATH', path)
      } catch (err) {
        console.log(err)
      }
    },
    async setCurRouteName ({commit}, name) {
      try {
        commit('SET_CUR_ROUTE_NAME', name)
      } catch (err) {
        console.log(err)
      }
    }
  },
  strict: process.env.NODE_ENV !== 'production'
})
